#!/usr/bin/env bash
# note - a simple note-taking application for the command-line
# Copyright ©2013-2023 Jason Trunks
# https://notabug.org/JasKinasis/note

# This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

# You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

NOTEVERSION="v1.0"
NOTEDIR="$HOME/.note"
NOTESDIR="$NOTEDIR/notes"
NOTECONFIG="$NOTEDIR/settings.cfg"
NO_ARGS_SO_LIST=0
HELP_CMD="-h"
REMOVE_CMD="-d"
RENAME_CMD="-r"
VIEW_CMD="-v"
LIST_BY_SIZE_CMD="-ls"
LIST_ALPHA_CMD="-la"
READ_CMD="-R"
BACKUP_NOTES_CMD="--backup"
SEARCH_CMD="-s"
SEARCH_CMD2="-S"
NOTE_BACKUP_FILE="$HOME/note-backup-$( date +%Y-%m-%d).tar.gz"
ED=
KEEPEDITORBACKUPS=
PAGER=

# Check for the existence of the .note directory in the users home dir
# and create it if it does not
if [ ! -d "$NOTEDIR" ]; then
	mkdir -p "$NOTEDIR"
fi

if [ ! -d "$NOTESDIR" ]; then
        mkdir -p "$NOTESDIR"
fi

# Check for the existence of the notesettings config file
# If not there, silently run notesettings to create default settings
if [ ! -f "$NOTECONFIG" ]; then
	notesettings 2&> /dev/null
fi

# Load the config file
. "$NOTECONFIG"

if [ $# -eq "$NO_ARGS_SO_LIST" ]; then # List notes
    echo "List of notes:"
    ls "$NOTESDIR"

elif [ "$1" = "$HELP_CMD" ]; then # Display help
    echo 'note - a simple cli note taking application'
    echo 'Copyright ©2013-2023 Jason Trunks'
	echo "Version: $NOTEVERSION"
    echo 'This program is free software licensed under the GPLV3 and comes with ABSOLUTELY NO WARRANTY.'
    echo;echo 'Usage:'
    echo 'note                          : List all notes'
    echo 'note [filename]               : Open a new or existing note in the editor'
    echo 'note -e [filename]            : Edit an existing note or create a new one'
    echo 'note -r [oldname] [newname]   : Rename a note'
    echo 'note -d [file1] [file2] [...] : Remove listed notes'
    echo 'note -v [filename]            : view a note in a pager (less, more, w3m etc)'
    echo 'note -la                      : Detailed list of notes, sorted alphabetically'
    echo 'note -ls                      : Detailed listof notes, sorted by size'
    echo 'note -R [file1] [file2] [...] : Read notes aloud via espeak'
	echo 'note --backup                 : Backup all notes and settings to a file in the users home directory'
	echo '                              : called "note-backup-YYYY-MM-DD.tar.gz"'
	echo 'note -s Foo                   : list notes containing "Foo" (case insensitive)'
	echo 'note -S Foo                   : list notes containing "Foo" (case sensitive)'
    echo 'note -h                       : Show this help/usage message'
    echo; echo 'See also notesettings (1)'
    echo 'notesettings is the configuration script for note and can be used to set'
    echo 'The cli text editor - default is $EDITOR'
    echo 'The cli pager - default is less'
    echo 'Whether to use backup files - default is 0 (no backup)'

elif [ "$1" = "$REMOVE_CMD" ]; then # Remove files
	shift
	for file in "$@"; do
        rm -f "$NOTESDIR/$file"
    done

elif [ "$1" = $RENAME_CMD ]; then # Rename a note
        if [ "$2" != "" ]; then
            if [ "$3" != "" ]; then
                mv "$NOTESDIR/$2" "$NOTESDIR/$3"
            fi
        else
            echo "Rename needs parameters"
        fi

elif [ "$1" = $VIEW_CMD ]; then # View a note in pager
	if [ "$2" != "" ] && [ -f "$NOTESDIR/$2" ]; then
		"$PAGER" "$NOTESDIR/$2"
	fi

elif [ "$1" = $LIST_BY_SIZE_CMD ]; then # list the contents of the note dir
	echo "Detailed list of notes sorted by size:"
	ls -AlhS "$NOTESDIR"

elif [ "$1" = $LIST_ALPHA_CMD ]; then
        echo "Detailed list of notes sorted by filename:"
        ls -Alh "$NOTESDIR"

elif [ "$1" = $READ_CMD ]; then
    if hash espeak &> /dev/null; then
        shift
        for file in "$@"; do
            espeak -p 99 -s 130 -v english_wmids "The note called $file contains the following text"
            espeak -p 99 -s 130 -v english_wmids < "$NOTESDIR/$file"
            sleep 1;
        done
   else
       echo "ERROR - The 'Read' feature requires espeak"
       exit 1
   fi

elif [ "$1" = "$BACKUP_NOTES_CMD" ]; then
	echo "Writing notes and configuration to: $NOTE_BACKUP_FILE"
	cd "$NOTEDIR"
	tar -czf "$NOTE_BACKUP_FILE" ./
	cd - &> /dev/null
elif [ "$1" = "$SEARCH_CMD" ]; then
	RESULT=$(grep -Rli "${2}" "$NOTESDIR/" | xargs -n 1 basename 2> /dev/null)
	if [ -z "$RESULT" ]; then
		echo "No notes contain \"$2\""
	else
		echo "notes containing \"$2\":"
		echo "$RESULT"
	fi
elif [ "$1" = "$SEARCH_CMD2" ]; then
	RESULT=$(grep -Rl "${2}" "$NOTESDIR/" | xargs -n 1 basename 2> /dev/null)
	if [ -z "$RESULT" ]; then
		echo "No notes contain \"$2\""
	else
		echo "notes containing \"$2\":"
		echo "$RESULT"
	fi
else #EDIT BY DEFAULT
    "$ED" "$NOTESDIR/$1"
	if [ -f "$NOTESDIR/$1~" ] && [ "$KEEPEDITORBACKUPS" == "0" ]; then
		rm "$NOTESDIR/$1~"
	fi
fi

exit 0

